<template>
  <div class="my">
    <div class="my-bg">
      <div class="title">个人中心</div>
    </div>
    <div class="content">
      <div class="userinfo">
        <u-avatar :src="userInfo.avatar_url || defaultAvatarUrl"></u-avatar>
        <div class="userinfo-right" v-if="userInfo.id">
          <div style="font-size: 38rpx">{{ userInfo.nickname }}</div>
          <div>{{ userInfo.phone || userInfo.email }}</div>
        </div>
        <div class="userinfo-right" v-else @click="login">
          登录/注册
        </div>
      </div>
      <div class="menu-box">
        <u-row gutter="20" style="width: 100%">
          <u-col span="3" v-for="item in menuList" :key="item.id">
            <div class="menu-item" @click="toPage(item)">
              <u-image width="100%" height="120" :src="item.src"></u-image>
              <div class="menu-name">{{ item.name }}</div>
            </div>
          </u-col>
        </u-row>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      defaultAvatarUrl: '',
      menuList: [
        {
          name: '我的订单',
          src: '/static/my/order.png',
          url: '/pages/order/index'
        },
        {
          name: '开具发票',
          src: '/static/my/order.png',
          url: '/pages/connact/invoice'
        },
        {
          name: '意见反馈',
          src: '/static/my/service.png',
          url: '/pages/connact/index'
        },
        {
          name: '联系客服',
          src: '/static/my/service.png'
        }
      ]
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  onShow() {
    if (this.userInfo.id) {
      this.$api.getUserInfo({
        id: this.userInfo.id
      }).then(res => {
        this.$store.commit('setUserInfo', res)
      })
    }
  },
  methods: {
    toPage(item) {
      if (item.url) {
        uni.navigateTo({
          url: item.url
        })
      } else {
        uni.showToast({
          title: '暂未开放',
          icon: 'none'
        })
      }
    },
    login() {
      uni.navigateTo({
        url: '/pages/user/login'
      })
    }
  }
}
</script>
<style lang="scss">
.my {
  .my-bg {
    height: 514rpx;
    background: linear-gradient(0deg, #F3F4F4 0%, #FF9346 27%, #FF770F 43%);
    display: flex;
    flex-direction: column;
    line-height: 140rpx;
  }
  .title {
    font-size: 32rpx;
    color: #ffffff;
    font-weight: bold;
    text-align: center;
  }
  .content {
    margin-top: -360rpx;
    padding: 0 24rpx;
    .userinfo {
      display: flex;
      align-items: center;
      .userinfo-right {
        padding: 0 24rpx;
        color: #ffffff;
      }
    }
    .menu-box {
      width: 100%;
      background: #FFFFFF;
      border-radius: 20rpx 20rpx 20rpx 20rpx;
      margin-top: 60rpx;
      padding: 16rpx;
      display: flex;
      .menu-item {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        font-size: 24rpx;
        color: #999999;
        line-height: 28rpx;
        margin-bottom: 20rpx;
      }
      .menu-name {
        margin-top: 10rpx;
      }
    }
  }
}
</style>
